1   /*
2    * Copyright (C) 2012 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import com.google.caliper.BeforeExperiment;
20  import com.google.caliper.Benchmark;
21  import com.google.caliper.Param;
22  import com.google.common.collect.BenchmarkHelpers.SetImpl;
23  
24  /**
25   * This is meant to be used with {@code --measureMemory} to measure the memory
26   * usage of various {@code Set} implementations.
27   * 
28   * @author Christopher Swenson
29   */
30  public class SetCreationBenchmark {
31    @Param({ "3", "6", "11", "23", "45", "91", "181", "362", "724", "1448",
32        "2896", "5793", "11585", "23170", "46341", "92682", "185364", "370728",
33        "741455", "1482910", "2965821", "5931642"})
34    private int size;
35    
36    // "" means no fixed seed
37    @Param("1234")
38    private SpecialRandom random;
39    
40    @Param({"Immutable", "Hash"})
41    private SetImpl impl;
42    
43    // the following must be set during setUp
44    private CollectionBenchmarkSampleData sampleData;
45    
46    @BeforeExperiment void setUp() {
47      sampleData = new CollectionBenchmarkSampleData(true, random, 0.8, size);
48    }
49    
50    @Benchmark int creation(int reps) {
51      int x = 0;
52      for (int i = 0; i < reps; i++) {
53        x ^= System.identityHashCode(impl.create(sampleData.getValuesInSet()));
54      }
55      return x;
56    }
57  }